<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveSupport::CoreExtensions::File::Atomic</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveSupport::CoreExtensions::File::Atomic</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../../files/vendor/rails/activesupport/lib/active_support/core_ext/file/atomic_rb.html">
                vendor/rails/activesupport/lib/active_support/core_ext/file/atomic.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000732">atomic_write</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000732" class="method-detail">
        <a name="M000732"></a>

        <div class="method-heading">
          <a href="#M000732" class="method-signature">
          <span class="method-name">atomic_write</span><span class="method-args">(file_name, temp_dir = Dir.tmpdir) {|temp_file| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Write to a file atomically. Useful for situations where you don&#8216;t
want other processes or threads to see half-written files.
</p>
<pre>
  File.atomic_write(&quot;important.file&quot;) do |file|
    file.write(&quot;hello&quot;)
  end
</pre>
<p>
If your temp directory is not on the same filesystem as the file
you&#8216;re trying to write, you can provide a different temporary
directory.
</p>
<pre>
  File.atomic_write(&quot;/data/something.important&quot;, &quot;/data/tmp&quot;) do |f|
    file.write(&quot;hello&quot;)
  end
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000732-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000732-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/file/atomic.rb, line 20</span>
20:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">atomic_write</span>(<span class="ruby-identifier">file_name</span>, <span class="ruby-identifier">temp_dir</span> = <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">tmpdir</span>)
21:           <span class="ruby-identifier">temp_file</span> = <span class="ruby-constant">Tempfile</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">basename</span>(<span class="ruby-identifier">file_name</span>), <span class="ruby-identifier">temp_dir</span>)
22:           <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">temp_file</span>
23:           <span class="ruby-identifier">temp_file</span>.<span class="ruby-identifier">close</span>
24: 
25:           <span class="ruby-keyword kw">begin</span>
26:             <span class="ruby-comment cmt"># Get original file permissions</span>
27:             <span class="ruby-identifier">old_stat</span> = <span class="ruby-identifier">stat</span>(<span class="ruby-identifier">file_name</span>)
28:           <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">ENOENT</span>
29:             <span class="ruby-comment cmt"># No old permissions, write a temp file to determine the defaults</span>
30:             <span class="ruby-identifier">check_name</span> = <span class="ruby-identifier">join</span>(<span class="ruby-identifier">dirname</span>(<span class="ruby-identifier">file_name</span>), <span class="ruby-node">&quot;.permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}&quot;</span>)
31:             <span class="ruby-identifier">open</span>(<span class="ruby-identifier">check_name</span>, <span class="ruby-value str">&quot;w&quot;</span>) { }
32:             <span class="ruby-identifier">old_stat</span> = <span class="ruby-identifier">stat</span>(<span class="ruby-identifier">check_name</span>)
33:             <span class="ruby-identifier">unlink</span>(<span class="ruby-identifier">check_name</span>)
34:           <span class="ruby-keyword kw">end</span>
35: 
36:           <span class="ruby-comment cmt"># Overwrite original file with temp file</span>
37:           <span class="ruby-identifier">rename</span>(<span class="ruby-identifier">temp_file</span>.<span class="ruby-identifier">path</span>, <span class="ruby-identifier">file_name</span>)
38: 
39:           <span class="ruby-comment cmt"># Set correct permissions on new file</span>
40:           <span class="ruby-identifier">chown</span>(<span class="ruby-identifier">old_stat</span>.<span class="ruby-identifier">uid</span>, <span class="ruby-identifier">old_stat</span>.<span class="ruby-identifier">gid</span>, <span class="ruby-identifier">file_name</span>)
41:           <span class="ruby-identifier">chmod</span>(<span class="ruby-identifier">old_stat</span>.<span class="ruby-identifier">mode</span>, <span class="ruby-identifier">file_name</span>)
42:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>